/*
 * ComerciosActualizarFrame.java
 *
 * Created on 5 de mayo de 2008, 12:19 PM
 */
package org.comercios.registros;

import org.mybeans.Sesion;
import org.mybeans.ComerciosBeans;
import org.mybeans.ContribuyentesBeans;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import javax.swing.JLabel;
import javax.swing.JOptionPane;
import org.menu.MenuComercios;  
import org.contribuyentes.ContribuyentesBusquedaFrame;
import org.jdesktop.swingx.autocomplete.AutoCompleteDecorator;

/**
 *
 * @author  avbravo
 */
public class ComerciosRegistrosFrame extends javax.swing.JFrame {

    Sesion sesion;
    ComerciosBeans comerciosBeans;
    ContribuyentesBeans contribuyentesBeans;
    MenuComercios menuComercios;
    boolean focoComercio = false;
    boolean focoContribuyente = false;

    /** Creates new form ComerciosActualizarFrame */
    public ComerciosRegistrosFrame() {
        initComponents();
        sesion = new Sesion();
        comerciosBeans = new ComerciosBeans();
        contribuyentesBeans = new ContribuyentesBeans();
         menuComercios = new MenuComercios();
        CargarCorregimientos(sesion.getIdmunicipio(), true);
        /*
         * Fondo
         */

         
         
         

        jPanelFondo.setOpaque(false);
        setContentPane(jPanelFondo);
        /*
         * fondo
         */
        AutoCompleteDecorator.decorate(this.jComboBoxCorregimiento);
        AutoCompleteDecorator.decorate(jComboBoxEstatal);
        jButtonEliminar.setEnabled(false);
        jButtonActualizar.setEnabled(false);

        if (sesion.getIdComercio() == null || sesion.getIdComercio().equals("")) {
        } else {
            jTextFieldIdComercio.setText(sesion.getIdComercio());
            ValidarComercios();
        }

    }

    /** This method is called from within the constructor to
     * initialize the form.
     * WARNING: Do NOT modify this code. The content of this method is
     * always regenerated by the Form Editor.
     */
    @SuppressWarnings("unchecked")
    // <editor-fold defaultstate="collapsed" desc="Generated Code">//GEN-BEGIN:initComponents
    private void initComponents() {

        jPanelFondo = new javax.swing.JPanel();
        jLabel1 = new javax.swing.JLabel();
        jLabel2 = new javax.swing.JLabel();
        jLabel3 = new javax.swing.JLabel();
        jLabel4 = new javax.swing.JLabel();
        jLabel5 = new javax.swing.JLabel();
        jLabel6 = new javax.swing.JLabel();
        jLabel7 = new javax.swing.JLabel();
        jLabel8 = new javax.swing.JLabel();
        jTextFieldIdComercio = new javax.swing.JTextField();
        jTextFieldComercio = new javax.swing.JTextField();
        jTextFieldRuc = new javax.swing.JTextField();
        jComboBoxCorregimiento = new javax.swing.JComboBox();
        jTextFieldDireccion = new javax.swing.JTextField();
        jTextFieldTelefono = new javax.swing.JTextField();
        jComboBoxEstatal = new javax.swing.JComboBox();
        jScrollPane3 = new javax.swing.JScrollPane();
        jTextAreaDescripcion = new javax.swing.JTextArea();
        jButtonBuscar = new javax.swing.JButton();
        jLabel10 = new javax.swing.JLabel();
        jTextFieldCedula = new javax.swing.JTextField();
        jButtonBuscarContribuyentes = new javax.swing.JButton();
        jLabel11 = new javax.swing.JLabel();
        jTextFieldNombre = new javax.swing.JTextField();
        jButtonActualizar = new javax.swing.JButton();
        jButtonLimpiar = new javax.swing.JButton();
        jButtonRegresar = new javax.swing.JButton();
        jButtonEliminar = new javax.swing.JButton();

        setDefaultCloseOperation(javax.swing.WindowConstants.DISPOSE_ON_CLOSE);
        setTitle("Registros de Comercios");
        addWindowFocusListener(new java.awt.event.WindowFocusListener() {
            public void windowGainedFocus(java.awt.event.WindowEvent evt) {
                formWindowGainedFocus(evt);
            }
            public void windowLostFocus(java.awt.event.WindowEvent evt) {
            }
        });

        jPanelFondo.setBorder(javax.swing.BorderFactory.createEtchedBorder());

        jLabel1.setText("Codigo");

        jLabel2.setText("Comercio");

        jLabel3.setText("Ruc");

        jLabel4.setText("Corregimiento");

        jLabel5.setText("Direccion");

        jLabel6.setText("Telefono");

        jLabel7.setText("Descripcion");

        jLabel8.setText("Estatal");

        jTextFieldIdComercio.setColumns(35);
        jTextFieldIdComercio.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldIdComercioActionPerformed(evt);
            }
        });

        jComboBoxEstatal.setModel(new javax.swing.DefaultComboBoxModel(new String[] { "No", "Si" }));

        jTextAreaDescripcion.setColumns(20);
        jTextAreaDescripcion.setRows(5);
        jScrollPane3.setViewportView(jTextAreaDescripcion);

        jButtonBuscar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscar.setToolTipText("Buscar");
        jButtonBuscar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarActionPerformed(evt);
            }
        });

        jLabel10.setText("Cedula");

        jTextFieldCedula.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jTextFieldCedulaActionPerformed(evt);
            }
        });

        jButtonBuscarContribuyentes.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/search.png"))); // NOI18N
        jButtonBuscarContribuyentes.setToolTipText("Buscar contribuyente");
        jButtonBuscarContribuyentes.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonBuscarContribuyentesActionPerformed(evt);
            }
        });

        jLabel11.setText("Nombre");

        jTextFieldNombre.setEditable(false);

        jButtonActualizar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/save.png"))); // NOI18N
        jButtonActualizar.setToolTipText("Actualizar");
        jButtonActualizar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonActualizarActionPerformed(evt);
            }
        });

        jButtonLimpiar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/escoba.png"))); // NOI18N
        jButtonLimpiar.setToolTipText("Limpiar");
        jButtonLimpiar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonLimpiarActionPerformed(evt);
            }
        });

        jButtonRegresar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/regresar.jpeg"))); // NOI18N
        jButtonRegresar.setToolTipText("Regresar");
        jButtonRegresar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonRegresarActionPerformed(evt);
            }
        });

        jButtonEliminar.setIcon(new javax.swing.ImageIcon(getClass().getResource("/gif/delete.png"))); // NOI18N
        jButtonEliminar.setToolTipText("Eliminar");
        jButtonEliminar.addActionListener(new java.awt.event.ActionListener() {
            public void actionPerformed(java.awt.event.ActionEvent evt) {
                jButtonEliminarActionPerformed(evt);
            }
        });

        javax.swing.GroupLayout jPanelFondoLayout = new javax.swing.GroupLayout(jPanelFondo);
        jPanelFondo.setLayout(jPanelFondoLayout);
        jPanelFondoLayout.setHorizontalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel4)
                            .addComponent(jLabel10)
                            .addComponent(jLabel11)
                            .addComponent(jLabel2)
                            .addComponent(jLabel1)
                            .addComponent(jLabel3))
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addGroup(jPanelFondoLayout.createSequentialGroup()
                                .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 173, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                                .addComponent(jButtonBuscar, javax.swing.GroupLayout.PREFERRED_SIZE, 66, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addComponent(jTextFieldComercio, javax.swing.GroupLayout.PREFERRED_SIZE, 461, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 301, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jComboBoxCorregimiento, javax.swing.GroupLayout.PREFERRED_SIZE, 399, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING, false)
                                .addComponent(jTextFieldRuc, javax.swing.GroupLayout.PREFERRED_SIZE, 246, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addGroup(jPanelFondoLayout.createSequentialGroup()
                                    .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, 137, javax.swing.GroupLayout.PREFERRED_SIZE)
                                    .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED, javax.swing.GroupLayout.DEFAULT_SIZE, Short.MAX_VALUE)
                                    .addComponent(jButtonBuscarContribuyentes, javax.swing.GroupLayout.PREFERRED_SIZE, 65, javax.swing.GroupLayout.PREFERRED_SIZE)))))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jLabel5)
                            .addComponent(jLabel6))
                        .addGap(44, 44, 44)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jTextFieldTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, 115, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jTextFieldDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, 388, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jLabel8)
                        .addGap(59, 59, 59)
                        .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                            .addComponent(jComboBoxEstatal, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                            .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)))
                    .addComponent(jLabel7)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGap(34, 34, 34)
                        .addComponent(jButtonActualizar, javax.swing.GroupLayout.PREFERRED_SIZE, 87, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(29, 29, 29)
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 81, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(33, 33, 33)
                        .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 74, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGap(39, 39, 39)
                        .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 78, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addContainerGap(58, Short.MAX_VALUE))
        );
        jPanelFondoLayout.setVerticalGroup(
            jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addGroup(jPanelFondoLayout.createSequentialGroup()
                .addContainerGap()
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel1)
                    .addComponent(jTextFieldIdComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jButtonBuscar))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jLabel2)
                    .addComponent(jTextFieldComercio, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel3)
                    .addComponent(jTextFieldRuc, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jLabel10)
                        .addGroup(jPanelFondoLayout.createSequentialGroup()
                            .addGap(2, 2, 2)
                            .addComponent(jButtonBuscarContribuyentes)))
                    .addComponent(jTextFieldCedula, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addGap(12, 12, 12)
                        .addComponent(jLabel11))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.UNRELATED)
                        .addComponent(jTextFieldNombre, javax.swing.GroupLayout.PREFERRED_SIZE, 27, javax.swing.GroupLayout.PREFERRED_SIZE)))
                .addGap(9, 9, 9)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel4)
                    .addComponent(jComboBoxCorregimiento, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel5)
                    .addComponent(jTextFieldDireccion, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.TRAILING)
                    .addComponent(jTextFieldTelefono, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
                    .addComponent(jLabel6))
                .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.BASELINE)
                    .addComponent(jLabel8)
                    .addComponent(jComboBoxEstatal, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(18, 18, 18)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jLabel7)
                        .addGap(47, 47, 47))
                    .addComponent(jScrollPane3, javax.swing.GroupLayout.PREFERRED_SIZE, 64, javax.swing.GroupLayout.PREFERRED_SIZE))
                .addGap(28, 28, 28)
                .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                    .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                        .addComponent(jButtonActualizar, javax.swing.GroupLayout.Alignment.TRAILING, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addGroup(jPanelFondoLayout.createSequentialGroup()
                            .addGap(1, 1, 1)
                            .addGroup(jPanelFondoLayout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
                                .addComponent(jButtonRegresar, javax.swing.GroupLayout.PREFERRED_SIZE, 38, javax.swing.GroupLayout.PREFERRED_SIZE)
                                .addComponent(jButtonLimpiar, javax.swing.GroupLayout.PREFERRED_SIZE, 37, javax.swing.GroupLayout.PREFERRED_SIZE))
                            .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                    .addGroup(jPanelFondoLayout.createSequentialGroup()
                        .addComponent(jButtonEliminar, javax.swing.GroupLayout.PREFERRED_SIZE, 39, javax.swing.GroupLayout.PREFERRED_SIZE)
                        .addPreferredGap(javax.swing.LayoutStyle.ComponentPlacement.RELATED)))
                .addGap(43, 43, 43))
        );

        javax.swing.GroupLayout layout = new javax.swing.GroupLayout(getContentPane());
        getContentPane().setLayout(layout);
        layout.setHorizontalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.PREFERRED_SIZE, javax.swing.GroupLayout.DEFAULT_SIZE, javax.swing.GroupLayout.PREFERRED_SIZE)
        );
        layout.setVerticalGroup(
            layout.createParallelGroup(javax.swing.GroupLayout.Alignment.LEADING)
            .addComponent(jPanelFondo, javax.swing.GroupLayout.PREFERRED_SIZE, 524, javax.swing.GroupLayout.PREFERRED_SIZE)
        );

        pack();
    }// </editor-fold>//GEN-END:initComponents

private void jButtonBuscarContribuyentesActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarContribuyentesActionPerformed

    ContribuyentesBusquedaFrame contribuyentesBusquedaFrame = new ContribuyentesBusquedaFrame();
    contribuyentesBusquedaFrame.setLocationRelativeTo(null);
    contribuyentesBusquedaFrame.setVisible(true);
    focoContribuyente = true;
    focoComercio = false;
}//GEN-LAST:event_jButtonBuscarContribuyentesActionPerformed

private void jTextFieldIdComercioActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldIdComercioActionPerformed
    ValidarComercios();

}//GEN-LAST:event_jTextFieldIdComercioActionPerformed

private void jButtonActualizarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonActualizarActionPerformed

    try {
        String cedula = this.jTextFieldCedula.getText();
        if (cedula == null || cedula.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese la cedula del contribuyente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldCedula.requestFocus();
            return;
        }

        String idComercio = this.jTextFieldIdComercio.getText();
        if (idComercio == null || idComercio.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el codigo de comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIdComercio.requestFocus();
            return;
        }
        String Comercio = this.jTextFieldComercio.getText();
        if (Comercio == null || Comercio.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el nombre de comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldComercio.requestFocus();
            return;
        }
        String direccion = this.jTextFieldDireccion.getText();
        if (direccion == null || direccion.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese la direccion del comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldDireccion.requestFocus();
            return;
        }
        String Corregimiento = (String) this.jComboBoxCorregimiento.getSelectedItem();
        if (Corregimiento == null || Corregimiento.equals("")) {
            JOptionPane.showMessageDialog(this, "seleccione un corregimiento", "Mensaje", JOptionPane.INFORMATION_MESSAGE);

            return;
        }
        String IdCorregimiento;
        IdCorregimiento = BuscarCorregimientos(Corregimiento);
        String estatal = (String) this.jComboBoxEstatal.getSelectedItem();
        if (estatal == null || estatal.equals("")) {
            JOptionPane.showMessageDialog(this, "seleccione si es o no estatal", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            return;
        }
        String ruc = this.jTextFieldRuc.getText();
        if (ruc == null) {
            ruc = "";
        }
        String descripcion = this.jTextAreaDescripcion.getText();
        if (descripcion == null) {
            descripcion = "";
        }
        String telefono = this.jTextFieldTelefono.getText();
        if (telefono == null) {
            telefono = "";
        }


        comerciosBeans.setIdComercio(idComercio);
        comerciosBeans.setComercio(Comercio);
        comerciosBeans.setCedula(cedula);
        comerciosBeans.setRuc(ruc);
        comerciosBeans.setDescripcion(descripcion);
        comerciosBeans.setDireccion(direccion);
        comerciosBeans.setIdCorregimiento(IdCorregimiento);
        comerciosBeans.setTelefono(telefono);
        comerciosBeans.setEstatal(estatal);
        comerciosBeans.Actualizar();

    } catch (Exception ex) {
        System.out.println("Error" + ex);
        JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonActualizarActionPerformed

private void jButtonLimpiarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonLimpiarActionPerformed

    Limpiar();
}//GEN-LAST:event_jButtonLimpiarActionPerformed

private void jButtonRegresarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonRegresarActionPerformed

    dispose();
}//GEN-LAST:event_jButtonRegresarActionPerformed

private void jButtonBuscarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonBuscarActionPerformed

    ComerciosBusquedaFrame comerciosBusquedaFrame = new ComerciosBusquedaFrame();
    comerciosBusquedaFrame.setLocationRelativeTo(null);
    comerciosBusquedaFrame.setVisible(true);
    focoComercio = true;
    focoContribuyente = false;
}//GEN-LAST:event_jButtonBuscarActionPerformed

private void jTextFieldCedulaActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jTextFieldCedulaActionPerformed
    ValidarCedula();

}//GEN-LAST:event_jTextFieldCedulaActionPerformed

private void formWindowGainedFocus(java.awt.event.WindowEvent evt) {//GEN-FIRST:event_formWindowGainedFocus
    // TODO add your handling code here:
    try {

        if (focoComercio) {
            CargarComercios();
        } else {
            if (focoContribuyente) {
                CargarContribuyentes();
            }

        }
        focoComercio = false;
        focoContribuyente = false;
    } catch (Exception ex) {
        sesion.MostrarMensajeError("Error ", "Clase: ComerciosActualizarFrame.java\n formWindowGainedFocus()\n Error: " + ex.getMessage().toString());
    }
}//GEN-LAST:event_formWindowGainedFocus

private void jButtonEliminarActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_jButtonEliminarActionPerformed

    try {
        String idComercio = this.jTextFieldIdComercio.getText();
        if (idComercio == null || idComercio.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el codigo ", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldIdComercio.requestFocus();
            return;
        }

        String comercio = this.jTextFieldComercio.getText();
        if (comercio == null || comercio.equals("")) {
            JOptionPane.showMessageDialog(this, "Ingrese el comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
            jTextFieldComercio.requestFocus();
            return;
        }

        comerciosBeans.setIdComercio(idComercio);
        if (comerciosBeans.Eliminar() == true) {
            Limpiar();
        }


    } catch (Exception ex) {

        JOptionPane.showMessageDialog(this, "Boton Eliminar\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
    }
}//GEN-LAST:event_jButtonEliminarActionPerformed

    /**
     * @param args the command line arguments
     */
    public static void main(String args[]) {
        java.awt.EventQueue.invokeLater(new Runnable() {

            public void run() {
                new ComerciosRegistrosFrame().setVisible(true);
            }
        });
    }

    private void ValidarComercios() {
        try {
            String IdComercio = this.jTextFieldIdComercio.getText();
            if (IdComercio == null || IdComercio.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese el codigo del comercio", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            comerciosBeans.setIdComercio(IdComercio);
            if (comerciosBeans.Consultar() == true) {
                CargarComercios();
            }



        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void ValidarCedula() {
        try {
            String cedula = this.jTextFieldCedula.getText();
            if (cedula == null || cedula.equals("")) {
                JOptionPane.showMessageDialog(this, "Ingrese la cedula del contribuyente", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            if (contribuyentesBeans.Consultar(cedula) == true) {
                CargarContribuyentes();
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Modulo:ValidarCedula()\nError " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private void CargarComercios() {
        try {
            if (comerciosBeans.getActivo().equals("no")) {
                JOptionPane.showMessageDialog(this, "El comercio no esta activo", "Mensaje", JOptionPane.WARNING_MESSAGE);
                return;
            }
            this.jTextFieldIdComercio.setText(comerciosBeans.getIdComercio());
            this.jTextFieldComercio.setText(comerciosBeans.getComercio());
            this.jTextFieldCedula.setText(comerciosBeans.getCedula());
            this.jTextFieldNombre.setText(comerciosBeans.getContribuyente());
            this.jTextFieldRuc.setText(comerciosBeans.getRuc());
            this.jTextAreaDescripcion.setText(comerciosBeans.getDescripcion());
            this.jTextFieldDireccion.setText(comerciosBeans.getDireccion());
            String corregimiento = BuscarCorregimientosCodigo(sesion.getIdmunicipio(), comerciosBeans.getIdCorregimiento());
            jComboBoxCorregimiento.setSelectedItem(corregimiento);
            this.jTextFieldTelefono.setText(comerciosBeans.getTelefono());
            jComboBoxEstatal.setSelectedItem(comerciosBeans.getEstatal());

            jButtonEliminar.setEnabled(false);
            jButtonActualizar.setEnabled(false);
            if (menuComercios.isBjMenuItemComerciosActualizar() == true) {
                jButtonActualizar.setEnabled(true);
            }
            if (menuComercios.isBjMenuItemComerciosEliminar() == true) {
                jButtonEliminar.setEnabled(true);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void CargarContribuyentes() {
        try {
            // ContribuyentesBeans contribuyentesBeans = new ContribuyentesBeans();

            this.jTextFieldCedula.setText(contribuyentesBeans.getCedula());
            this.jTextFieldNombre.setText(contribuyentesBeans.getContribuyente());
            jButtonEliminar.setEnabled(false);
            jButtonActualizar.setEnabled(false);
            if (menuComercios.isBjMenuItemComerciosActualizar() == true) {
                jButtonActualizar.setEnabled(true);
            }
            if (menuComercios.isBjMenuItemComerciosEliminar() == true) {
                jButtonEliminar.setEnabled(true);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error " + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    public void CargarCorregimientos() {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            String sqlquery = "select idcorregimiento, corregimiento  from corregimients where idmunicipio = ?";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            ResultSet rs = psquery.executeQuery();
            jComboBoxCorregimiento.removeAllItems();
            int count = 0;
            while (rs.next()) {

                String corregimiento = rs.getString("corregimiento");
                jComboBoxCorregimiento.insertItemAt(corregimiento, count);
                count++;
            }
            if (count != 0) {
                jComboBoxCorregimiento.setSelectedIndex(0);
            }
        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error Cargando Corregimientos" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }

    private String BuscarCorregimientos(String corregimiento) {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return "";
            }
            String sqlquery = "select * from corregimients where idmunicipio = ? and corregimiento =?";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, sesion.getIdmunicipio());
            psquery.setString(2, corregimiento);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();

            if (x == 0) {
                JOptionPane.showMessageDialog(this, "no existe un corregimiento con ese nombre", "Mensaje", JOptionPane.INFORMATION_MESSAGE);

                return "";
            }
            return rs.getString("idcorregimiento");


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error Cargando Corregimientos" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        return "";
    }

    private void Limpiar() {
        this.jTextFieldIdComercio.setText("");
        this.jTextFieldComercio.setText("");
        this.jTextFieldDireccion.setText("");
        this.jTextFieldRuc.setText("");
        this.jTextAreaDescripcion.setText("");
        this.jTextFieldTelefono.setText("");
        jTextFieldCedula.setText("");
        jTextFieldNombre.setText("");
        jButtonEliminar.setEnabled(false);
        jButtonActualizar.setEnabled(false);
    }

    private String BuscarCorregimientosCodigo(String idMunicipio, String corregimiento) {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return "";
            }
            String sqlquery = "select * from corregimients where idmunicipio = ? and idcorregimiento =?";
            PreparedStatement psquery = sesion.con.prepareStatement(sqlquery);
            psquery.setString(1, idMunicipio);
            psquery.setString(2, corregimiento);
            ResultSet rs = psquery.executeQuery();
            rs.next();
            int x = rs.getRow();

            if (x == 0) {
                JOptionPane.showMessageDialog(this, "no existe un corregimiento con ese codigo", "Mensaje", JOptionPane.INFORMATION_MESSAGE);

                return "";
            }
            return rs.getString("corregimiento");


        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error Cargando Corregimientos" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
        return "";
    }

    public void CargarCorregimientos(String idMunicipio, boolean comboCorregimientoActual) {
        try {
            if (sesion.Conectar() == false) {
                JOptionPane.showMessageDialog(this, "No se pudo establecer la conexion", "Mensaje", JOptionPane.INFORMATION_MESSAGE);
                return;
            }
            PreparedStatement psquery;
            if (idMunicipio.equals("")) {
                //cuando se quiere mostrar todos los corregimientos
                String sqlquery = "select idcorregimiento, corregimiento  from corregimients ";
                psquery = sesion.con.prepareStatement(sqlquery);

            } else {
                String sqlquery = "select idcorregimiento, corregimiento  from corregimients where idmunicipio = ?";
                psquery = sesion.con.prepareStatement(sqlquery);
                psquery.setString(1, idMunicipio);
            }
            ResultSet rs = psquery.executeQuery();
            jComboBoxCorregimiento.removeAllItems();


            int count = 0;
            while (rs.next()) {
                String corregimiento = rs.getString("corregimiento");
                jComboBoxCorregimiento.insertItemAt(corregimiento, count);
                count++;
            }
            if (count != 0) {
                jComboBoxCorregimiento.setSelectedIndex(0);
            }

        } catch (Exception ex) {
            JOptionPane.showMessageDialog(this, "Error Cargando Corregimientos" + ex.getMessage(), "Error", JOptionPane.ERROR_MESSAGE);
        }
    }
    // Variables declaration - do not modify//GEN-BEGIN:variables
    private javax.swing.JButton jButtonActualizar;
    private javax.swing.JButton jButtonBuscar;
    private javax.swing.JButton jButtonBuscarContribuyentes;
    private javax.swing.JButton jButtonEliminar;
    private javax.swing.JButton jButtonLimpiar;
    private javax.swing.JButton jButtonRegresar;
    private javax.swing.JComboBox jComboBoxCorregimiento;
    private javax.swing.JComboBox jComboBoxEstatal;
    private javax.swing.JLabel jLabel1;
    private javax.swing.JLabel jLabel10;
    private javax.swing.JLabel jLabel11;
    private javax.swing.JLabel jLabel2;
    private javax.swing.JLabel jLabel3;
    private javax.swing.JLabel jLabel4;
    private javax.swing.JLabel jLabel5;
    private javax.swing.JLabel jLabel6;
    private javax.swing.JLabel jLabel7;
    private javax.swing.JLabel jLabel8;
    private javax.swing.JPanel jPanelFondo;
    private javax.swing.JScrollPane jScrollPane3;
    private javax.swing.JTextArea jTextAreaDescripcion;
    private javax.swing.JTextField jTextFieldCedula;
    private javax.swing.JTextField jTextFieldComercio;
    private javax.swing.JTextField jTextFieldDireccion;
    private javax.swing.JTextField jTextFieldIdComercio;
    private javax.swing.JTextField jTextFieldNombre;
    private javax.swing.JTextField jTextFieldRuc;
    private javax.swing.JTextField jTextFieldTelefono;
    // End of variables declaration//GEN-END:variables
}
